<?php

namespace app\api\controller;

use app\api\model\Sign as ModelSign;
use app\api\model\User;
use app\common\LogService;
use app\common\model\ScoreLog as ModelScoreLog;
use app\common\Notification;
use app\common\ScoreLog;
use think\facade\Db;

class Sign extends Base
{
    protected $score = [
        1 => 5,
        2 => 10,
        3 => 15,
        4 => 30,
        5 => 50,
        6 => 50,
        7 => 100,
    ];
    protected $exp = [
        1 => 10,
        2 => 20,
        3 => 30,
        4 => 50,
        5 => 50,
        6 => 50,
        7 => 100,
    ];
    public function do()
    {
        $isEmpty = ModelSign::where(['user_id' => $this->uid]);
        $user = Db::name('user')->where(['id' => $this->uid])->find();
        if ($isEmpty->findOrEmpty()->isEmpty()) {
            return $this->firstSign($user);
        } else {
            $sign = $isEmpty->find();
            if ($sign['sign_date'] != date('d')) {
                $sign_num = $sign['sign_num'];
                Db::startTrans();
                if ($sign['sign_date'] + 1 != date('d')) {
                    $sco = $this->score[1];
                    $score = $user['score'] + $sco;
                    $exp = $user['exp'] + $this->exp[1];
                    ModelSign::where(['user_id' => $this->uid])->update(['sign_num' => ++$sign_num, 'sign_date' => date('d'), 'continuous_num' => 1]);
                } else {
                    $sco = $this->getValue($sign['continuous_num'])['score'];
                    $score = $user['score'] + $sco;
                    $exp = $user['exp'] + $this->getValue($sign['continuous_num'])['exp'];
                    $continuous_num =  $sign['continuous_num'];
                    ModelSign::where(['user_id' => $this->uid])->update(['sign_num' => ++$sign_num, 'sign_date' => date('d'), 'continuous_num' => ++$continuous_num]);
                }
                try {
                    Db::name('user')->where(['id' => $this->uid])->update(['score' => $score, 'exp' => $exp]);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                }
                LogService::record(
                    user_id: getUid(),
                    name: '用户签到',
                );
                $content = '签到奖励：获得积分' . $this->getValue($sign['continuous_num'])['score'] . ',经验' . $this->getValue($sign['continuous_num'])['exp'];
                Notification::send($this->uid, $content, 1);
                ScoreLog::add($this->uid, $sco, ModelScoreLog::TYPE_SIGN, '签到奖励', $content);
                return success([], '签到成功，奖励已发放到你的账户');
            } else  return error('每日只能签到一次哦!');
        }
    }
    // 签到排名
    public function list()
    {
        $type = input('type', 'sign_num');
        $arr = ['sign_num', 'continuous_num'];
        if (in_array($type, $arr)) {
            $list = ModelSign::order("$type desc")->limit(10)->select();
            return success($list);
        }
    }
    public function firstSign($user)
    {
        Db::startTrans();
        try {
            ModelSign::create(['user_id' => $this->uid, 'sign_date' => date('d')]);
            $score = $user['score'] + $this->score[1];
            $exp = $user['exp'] + $this->exp[1];
            Db::name('user')->where(['id' => $this->uid])->update(['score' => $score, 'exp' => $exp]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        $content =  '签到成功，获得积分' . $this->score[1] . ',经验' . $this->exp[1];
        ScoreLog::add($this->uid, $this->score[1], ModelScoreLog::TYPE_SIGN, '签到奖励', $content);
        Notification::send($this->uid, $content, 1);
        return success([], '签到成功，奖励已发放到你的账户');
    }
    public function getValue($continuous)
    {
        if ($continuous + 1 >= 7) {
            $score = $this->score[7];
            $exp = $this->exp[7];
        } else {
            $score = $this->score[$continuous + 1];
            $exp = $this->exp[$continuous  + 1];
        }
        return ['score' => $score, 'exp' => $exp];
    }
}
